import config from '@/views/tankGame/config';

interface IpositionCollection {
  x: number;
  y: number;
}

export const globalPosCol: IpositionCollection[] = [];

// 随机位置
function randomPosition(modelWidth: number, modelHeight: number, isTank: boolean) {
  return {
    x: Math.floor(Math.random() * (config.canvas.width / modelWidth)) * modelWidth,
    y: isTank ? 0 : Math.floor(Math.random() * (config.canvas.height / modelHeight - 6)) * modelHeight + modelHeight * 2,
  };
}

// 避免位置重复
export function getPositionCollection(num: number, isTank: boolean = false) {
  const positionCollection: IpositionCollection[] = [];
  for (let i = 0; i < num; i++) {
    const position = randomPosition(config.model.width, config.model.height, isTank);
    const isExist =
      positionCollection.some((pos) => pos.x === position.x && pos.y === position.y) &&
      globalPosCol.some((pos) => pos.x === position.x && pos.y === position.y);
    if (!isExist) {
      positionCollection.push(position);
      globalPosCol.push(position);
    } else num += 1;
  }
  return positionCollection;
}
